package mysql

import (
	"fmt"
	"github.com/zeromicro/go-zero/core/logx"
	"goodsrpc/appconfig"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

var (
	err error
	DB  *gorm.DB
)

func Init() {
	a := appconfig.AppCon.MysqlConfig
	// 参考 https://github.com/go-sql-driver/mysql#dsn-data-source-name 获取详情
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", a.User, a.Password, a.Host, a.Post, a.Database)
	DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic(err)
	}
	logx.Info("数据连接成功")
	err = DB.AutoMigrate(&User{}, &RealName{}, &House{}, &Order{})
	if err != nil {
		panic(err)
	}
	logx.Info("数据迁移成功")
}
